using JT808Protocol.Models.Enums;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy
{
    /// <summary>
    /// 透传数据
    /// </summary>
    public class DrPassthrough_0x0900 : IJT808Body
    {
        /// <summary>
        /// 消息类型
        /// </summary>
        [JsonPropertyName("消息类型")] public EnumPassthroughType DataType { get; set; }

        /// <summary>
        /// 消息列表总数
        /// </summary>
        [JsonPropertyName("消息列表总数")] public byte DataCount { get; set; }

        /// <summary>
        /// 透传外设备消息列表
        /// </summary>
        [JsonPropertyName("透传外设备消息列表")] public List<object> PeripheralList { get; set; }

        public override IJT808Body ReadBody(ReadOnlySpan<byte> source, ref int index)
        {
            var typeByte = source[index];
            if (!Enum.IsDefined(typeof(EnumPassthroughType), typeByte))
            {
                DataType = EnumPassthroughType.NoDefine;
                return null;
            }
            DataType = (EnumPassthroughType)typeByte;

            index++;
            DataCount = source[index];

            if (DataCount < 1)
            {
                return null;
            }

            PeripheralList = new List<object>();
            // todo:考虑改变方法，有可能长度失控
            while (index < source.Length - 2)
            {
                dynamic per = null;
                switch (DataType)
                {
                    case EnumPassthroughType.GnssData_0x00:
                        break;

                    case EnumPassthroughType.RoadIc_0x0B:
                        break;

                    case EnumPassthroughType.Com1_0x41:
                        break;

                    case EnumPassthroughType.Com2_0x42:
                        break;

                    case EnumPassthroughType.PeripheralStatus_0xF7:
                        per = new ItemPeripheralStatus();
                        break;

                    case EnumPassthroughType.PeripheralInfo_0xF8:
                        per = new ItemPeripheralInfo();
                        break;

                    case EnumPassthroughType.NoDefine:
                        break;

                    default:
                        break;
                }

                if (per == null)
                {
                    //Logger.Warn($"暂不支持的外设类型:{BitConverter.ToString(source, 0, 1)}");
                    break;
                }

                if (per is ItemPeripheralStatus)
                {
                    ((ItemPeripheralStatus)per).ReadBody(source, ref index);
                }
                else if (per is ItemPeripheralInfo)
                {
                    ((ItemPeripheralInfo)per).ReadBody(source, ref index);
                }
                index += per.DataLength;
            }

            return this;
        }

        public override List<byte> WriteToBytes()
        {
            throw new NotImplementedException();
        }
    }
}